import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A scanner which will generate tokens given descriptions.
 * 
 * @author Chen Chen
 * 
 */
public class TokenGenerator {
	

	/**
	 * Generates a stream of tokens from string of characters. Return null if an error occurred.
	 * 
	 * @param input source code, string of characters
	 * @param tokenDescriptionMap a map maps token description, written in regular expression, to token name
	 * @param keywords a list of keywords
	 * @return string of tokens or null if an error occured
	 */
	public static String parse(String input,Map<String,String> tokenDescriptionMap,List<String>keywords) {
		StringBuilder out = new StringBuilder();
		input = input.trim();
		while (input.length() > 0) {
			boolean matched=false;
			
			
			//match token
			for (String desc : tokenDescriptionMap.keySet()) {
				Pattern p=Pattern.compile(desc);
				Matcher m = p.matcher(input);
				if(m.lookingAt()){
					
					out.append(' ');
					out.append(keywords.contains(input.substring(0, m.end()))?input.substring(0, m.end()):tokenDescriptionMap.get(desc));
					out.append(' ');
					input=input.substring(m.end()).trim();
					matched=true;
					break;
				}
			}
			if(!matched){
				System.out.println("error matching: "+ input);
				return null;
			}
			input.trim();
		}
		return out.toString()+" $";
	}

}
